Follow through on the OUT_DIR promise for rustc compiles
authorAlex Crichton <alex@alexcrichton.com>
Tue, 4 Nov 2014 07:20:19 +0000 (23:20 -0800)
committerAlex Crichton <alex@alexcrichton.com>
Wed, 5 Nov 2014 19:37:34 +0000 (11:37 -0800)
src/cargo/ops/cargo_rustc/mod.rs
tests/test_cargo_compile_custom_build.rs

index be5dcf86e8afa5a78ce8023181ad6e2fa66963b5..647fb0e6f41ebb83e495632a57cb3985d2026854 100644 (file)
@@ -582,6 +582,15 @@ fn build_deps_args(mut cmd: ProcessBuilder, target: &Target, package: &Package,
     cmd = cmd.arg("-L").arg(layout.root());
     cmd = cmd.arg("-L").arg(layout.deps());
 
+    let has_build_cmd = package.get_targets().iter().any(|t| {
+        t.get_profile().is_custom_build()
+    });
+    cmd = cmd.env("OUT_DIR", if has_build_cmd {
+        Some(layout.build_out(package))
+    } else {
+        None
+    });
+
     // Traverse the entire dependency graph looking for -L paths to pass for
     // native dependencies.
     // OLD-BUILD: to-remove
index e8d83c3f5ff273c93544ab43d3c8401f610e4e61..11c5b56880015f771bff5d09f583e4d5fe1bb3b8 100644 (file)
@@ -753,3 +753,42 @@ test!(output_separate_lines {
 {running} `rustc [..] --crate-name foo [..] -L foo -l foo:static`
 ", compiling = COMPILING, running = RUNNING).as_slice()));
 })
+
+test!(code_generation {
+    let p = project("foo")
+        .file("Cargo.toml", r#"
+            [project]
+            name = "foo"
+            version = "0.5.0"
+            authors = []
+            build = "build.rs"
+        "#)
+        .file("src/main.rs", r#"
+            include!(concat!(env!("OUT_DIR"), "/hello.rs"))
+
+            fn main() {
+                println!("{}", message());
+            }
+        "#)
+        .file("build.rs", r#"
+            use std::os;
+            use std::io::File;
+
+            fn main() {
+                let dst = Path::new(os::getenv("OUT_DIR").unwrap());
+                let mut f = File::create(&dst.join("hello.rs")).unwrap();
+                f.write_str("
+                    pub fn message() -> &'static str {
+                        \"Hello, World!\"
+                    }
+                ").unwrap();
+            }
+        "#);
+    assert_that(p.cargo_process("run"),
+                execs().with_status(0)
+                       .with_stdout(format!("\
+{compiling} foo v0.5.0 (file://[..])
+{running} `target[..]foo`
+Hello, World!
+", compiling = COMPILING, running = RUNNING).as_slice()));
+})